package jee.boot.module.rest;

import io.swagger.annotations.ApiOperation;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.websocket.WebsocketMessage;
import jee.boot.module.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 为其他业务提供restfull接口用于发送websocket消息
 */
@RestController
@RequestMapping(value = "/websocket")
public class WebSocketRestController {
    @Autowired
    private WebSocketService service;

    /**
     * 发送通知消息
     *
     * @param entity
     * @return
     */
    @PostMapping("/send")
    @ApiOperation(value = "发送通知消息")
    public ResponseMessage<WebsocketMessage> send(@RequestBody WebsocketMessage entity) {
        if (StringUtils.isNotBlank(entity.getSid())) {
            service.sendMessage(entity.getSid(), entity.getMsg());
        } else {
            service.sendMessage(entity.getMsg());
        }
        // 返回结果有信息
        return Result.success();
    }
}
